#pragma once

#include <stdio.h>
#include <windows.h>
#include <dinput.h>
#include <conio.h>
#include <string>
#include <fstream>
#include <cmath>

#include "winmain.h"
#include "winmain2.h"
#include "globals.h"
#include "globals2.h"
#include "Object.h"
#include "Unit.h"
#include "Env.h"
#include "Misc.h"

using namespace std;

// include the DirectX Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dxerr.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
#pragma comment (lib, "winmm.lib")

/*****************************
Function Prototypes
*****************************/
void update(void);
void updateObjectToCamDistance(void);
void updateDrawingSequenceList(void);
bool compareFunction(float, float);
bool compareFunctionUnit(Unit, Unit);
bool compareFunctionEnv(Env, Env);
bool compareFunctionMisc(Misc, Misc);

void drawMenuUI(void);
void drawUnitsUI(void);
void drawEnvUI(void);
void drawMiscUI(void);
void drawLightUI(void);

void drawUnitModelIcons(void);
void drawEnvModelIcons(void);
void drawMiscModelIcons(void);

void drawDisplayOnOffSwitches(void);
void drawOpenMap(void);
void drawSaveMapAs(void);
void drawSavingMap(void);

void drawUnitInfoText(void);
void drawEnvInfoText(void);
void drawMiscInfoText(void);
void drawLightInfoText(void);
void drawInfoText(void);

void handleInput(void);
void handleLeftMousePress(void);
void handleRightMousePress(void);

void topMenuButtonFunctions(BUTTON_CMD);
void unitButtonFunctions(BUTTON_CMD);
void envButtonFunctions(BUTTON_CMD);
void miscButtonFunctions(BUTTON_CMD);
void lightButtonFunctions(BUTTON_CMD);
void unitModelButtonFunctions(BUTTON_CMD);
void envModelButtonFunctions(BUTTON_CMD);
void miscModelButtonFunctions(BUTTON_CMD);
void displayOnOffSwitchButtonFunctions(BUTTON_CMD);
void openMapButtonFunctions(BUTTON_CMD);

void createCamera(float, float);
void moveCamera(D3DXVECTOR3);
void pointCamera(D3DXVECTOR3);

void initializeLight(void);

bool isPointInRect(INTPair, RECT);

bool mousePicking(void);
bool mousePicking2(void);

void inputSaveMapAs(void);
void setOpenFilesList(void);
void resetOpenFileButtons(void);
void eraseLists(void);

// importing/exporting maps
void saveToFile(void);
void openMap(string);

/*****************************
Functions
*****************************/
void update(void) {
	if(cState == SAVING_MAP) {
		saveToFile();
		cState = CLEAR;
	}

	updateObjectToCamDistance();
	/*if(updateDrawingCounter == 0) {
		updateObjectToCamDistance();
		updateDrawingSequenceList();
		updateDrawingCounter++;
	} else {
		updateDrawingCounter++;
	}
	if(updateDrawingCounter >= UPDATE_DRAWING_COUNTER_MAX) {
		updateDrawingCounter = 0;
	}*/
}

void updateObjectToCamDistance(void) {
	if(displayUnitList) {
		list<Unit>::iterator uI = UnitList.begin();
		for(;uI != UnitList.end(); uI++) {
			float dist = pow((position.x-uI->getPosition().x),2) + pow((position.y-uI->getPosition().y),2) 
				+ pow((position.z-uI->getPosition().z),2);
			dist = pow(dist, 0.5f); // instead of sqrt(), use pow(x,0.5) for speed
			uI->setDistToCam(dist);
		}
	}

	if(displayEnvList) {
		list<Env>::iterator eI = EnvList.begin();
		for(;eI != EnvList.end(); eI++) {
			float dist = pow((position.x-eI->getPosition().x),2) + pow((position.y-eI->getPosition().y),2) 
				+ pow((position.z-eI->getPosition().z),2);
			dist = pow(dist, 0.5f); // instead of sqrt(), use pow(x,0.5) for speed
			eI->setDistToCam(dist);
		}
	}

	if(displayMiscList) {
		list<Misc>::iterator mI = MiscList.begin();
		for(;mI != MiscList.end(); mI++) {
			float dist = pow((position.x-mI->getPosition().x),2) + pow((position.y-mI->getPosition().y),2) 
				+ pow((position.z-mI->getPosition().z),2);
			dist = pow(dist, 0.5f); // instead of sqrt(), use pow(x,0.5) for speed
			mI->setDistToCam(dist);
		}
	}
}

void updateDrawingSequenceList(void) {
	list<float> distComparison;
	list<Unit>::iterator uI = UnitList.begin();
	list<Env>::iterator eI = EnvList.begin();
	list<Misc>::iterator mI = MiscList.begin();
	drawingSequenceType.clear();

	if(displayUnitList) {
		UnitList.sort(compareFunctionUnit);
		UnitList.reverse();
		for(;uI != UnitList.end(); uI++)
			distComparison.push_back(uI->getDistToCam());
	}
	if(displayEnvList) {
		EnvList.sort(compareFunctionEnv);
		EnvList.reverse();
		for(;eI != EnvList.end(); eI++)
			distComparison.push_back(eI->getDistToCam());
	}
	if(displayMiscList) {
		MiscList.sort(compareFunctionMisc);
		MiscList.reverse();
		for(;mI != MiscList.end(); mI++)
			distComparison.push_back(mI->getDistToCam());
	}

	distComparison.sort(compareFunction);
	distComparison.reverse();

	bool found = false;
	list<float>::iterator dC = distComparison.begin();
	for(;dC != distComparison.end(); dC++) {
		if(displayUnitList && !found) {
			for(uI = UnitList.begin(); uI != UnitList.end(); uI++) {
				if(uI->getDistToCam() == *dC) {
					drawingSequenceType.push_back('U');
					found = true;
				}
			}
		}
		if(displayEnvList && !found) {
			for(eI = EnvList.begin(); eI != EnvList.end(); eI++) {
				if(eI->getDistToCam() == *dC) {
					drawingSequenceType.push_back('E');
					found = true;
				}
			}
		}
		if(displayMiscList && !found) {
			for(mI = MiscList.begin(); mI != MiscList.end(); mI++) {
				if(mI->getDistToCam() == *dC) {
					drawingSequenceType.push_back('M');
					found = true;
				}
			}
		}
		found = false;
	}
	distComparison.clear();
}

bool compareFunction(float first, float second) {
	if(first < second) {
		return true;
	} else {
		return false;
	}
}

bool compareFunctionUnit(Unit first, Unit second) {
	if(first.getDistToCam() < second.getDistToCam()) {
		return true;
	} else {
		return false;
	}
}
bool compareFunctionEnv(Env first, Env second) {
	if(first.getDistToCam() < second.getDistToCam()) {
		return true;
	} else {
		return false;
	}
}
bool compareFunctionMisc(Misc first, Misc second) {
	if(first.getDistToCam() < second.getDistToCam()) {
		return true;
	} else {
		return false;
	}
}

void drawUI(void) {
	drawMenuUI();
	if(cState != UNIT_SELECTED && cState != ENV_SELECTED && cState != MISC_SELECTED)
		drawDisplayOnOffSwitches();
	if(cState == UNITS_MENU)
		drawUnitModelIcons();
	if(cState == UNITS_MENU || cState == UNIT_SELECTED)
		drawUnitsUI();
	if(cState == UNIT_SELECTED)
		drawUnitInfoText();
	if(cState == ENV_MENU)
		drawEnvModelIcons();
	if(cState == ENV_MENU || cState == ENV_SELECTED)
		drawEnvUI();
	if(cState == ENV_SELECTED)
		drawEnvInfoText();
	if(cState == MISC_MENU)
		drawMiscModelIcons();
	if(cState == MISC_MENU || cState == MISC_SELECTED)
		drawMiscUI();
	if(cState == MISC_SELECTED)
		drawMiscInfoText();
	if(cState == LIGHT_MENU) {
		drawLightUI();
		drawLightInfoText();
	}
	if(cState == OPENING_MAP)
		drawOpenMap();
	if(cState == SAVING_MAP_AS)
		drawSaveMapAs();
	if(cState == SAVING_MAP)
		drawSavingMap();
	drawInfoText();
}

void drawMenuUI(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/MenuUIBG.png", 1200, 150, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3(0,5,0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();
	
	list<BUTTON>::iterator iter = MenuButtons.begin();
	for(;iter != MenuButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();
}

void drawUnitsUI(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);

	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/SubMenuUIBG.png", 1200, 200, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)0,(float)UnitButtons.back().y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	list<BUTTON>::iterator iter = UnitButtons.begin();
	for(;iter != UnitButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();

	iter = UnitButtons.begin();
	for(;iter != UnitButtons.end(); iter++) {
		if(iter->bCmd == PLACE_OBJECT) {
			RECT font_rect = {iter->x + SUBMENU_BUTTON_WIDTH,iter->y+5,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Place Obj.",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == DELETE_OBJECT) {
			RECT font_rect = {iter->x + SUBMENU_BUTTON_WIDTH,iter->y+5,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Delete Obj.",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LOCATION_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Location (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ROTATION_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Rotation (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_SCALE_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Scale (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LEVEL) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Level",						// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ALIGNMENT) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Alignment",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_VALUE) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Value",						// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_CAN_MOVE) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Can Move?",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_STANCE) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Stance",						// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_GROUP) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Group",						// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
	}
}

void drawEnvUI(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);

	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/SubMenuUIBG.png", 1200, 200, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)0,(float)EnvButtons.back().y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	list<BUTTON>::iterator iter = EnvButtons.begin();
	for(;iter != EnvButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();

	iter = EnvButtons.begin();
	for(;iter != EnvButtons.end(); iter++) {
		if(iter->bCmd == PLACE_OBJECT) {
			RECT font_rect = {iter->x + SUBMENU_BUTTON_WIDTH,iter->y+5,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Place Obj.",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == DELETE_OBJECT) {
			RECT font_rect = {iter->x + SUBMENU_BUTTON_WIDTH,iter->y+5,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Delete Obj.",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LOCATION_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Location (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ROTATION_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Rotation (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_SCALE_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Scale (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ALIGNMENT) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Alignment",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_VALUE) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Value",						// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
	}
}

void drawMiscUI(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);

	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/SubMenuUIBG.png", 1200, 200, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)0,(float)MiscButtons.back().y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	list<BUTTON>::iterator iter = MiscButtons.begin();
	for(;iter != MiscButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();

	iter = MiscButtons.begin();
	for(;iter != MiscButtons.end(); iter++) {
		if(iter->bCmd == PLACE_OBJECT) {
			RECT font_rect = {iter->x + SUBMENU_BUTTON_WIDTH,iter->y+5,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Place Obj.",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == DELETE_OBJECT) {
			RECT font_rect = {iter->x + SUBMENU_BUTTON_WIDTH,iter->y+5,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Delete Obj.",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LOCATION_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Location (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ROTATION_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Rotation (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_SCALE_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Scale (X,Y,Z)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ALIGNMENT) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"For",							// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_PURPOSE) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Purpose",						// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_HRADIUS) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Horizontal Radius (beacon)",	// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_ORDER) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Order# (waypoint)",			// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_GROUP) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Group (waypoint)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_SOUNDFILE) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Sound File",					// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
	}
}

void drawLightUI(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);

	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/SubMenuUIBG.png", 1200, 200, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)0,(float)MiscButtons.back().y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	list<BUTTON>::iterator iter = LightButtons.begin();
	for(;iter != LightButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();

	iter = LightButtons.begin();
	for(;iter != LightButtons.end(); iter++) {
		if(iter->bCmd == PLUS_LIGHT_DIR_X) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Direction Nmlz'd (X,Y,Z)",		// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LIGHT_DIF_R) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Diffuse (R,G,B)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LIGHT_SPE_R) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Specular (R,G,B)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
		if(iter->bCmd == PLUS_LIGHT_AMB_R) {
			RECT font_rect = {iter->x + (long)(SUBMENU_BUTTON_WIDTH/2),iter->y-font.tmHeight-2,0,0};
			standardFont->DrawText(NULL,						// Sprite
								"Ambient (R,G,B)",				// String
								-1,								// Count
								&font_rect,						// Rect
								FONT_FORMAT,					// Format
								FONT_COLOR);					// Color
		}
	}
}

void drawUnitModelIcons(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	list<BUTTON>::iterator iter = UnitSideListButtons.begin();
	for(;iter != UnitSideListButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();
}

void drawEnvModelIcons(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	list<BUTTON>::iterator iter = EnvSideListButtons.begin();
	for(;iter != EnvSideListButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();
}

void drawMiscModelIcons(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	list<BUTTON>::iterator iter = MiscSideListButtons.begin();
	for(;iter != MiscSideListButtons.end(); iter++) {
		sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();
}

void drawDisplayOnOffSwitches(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	list<BUTTON>::iterator iter = DisplayOnOffSwitch.begin();
	for(;iter != DisplayOnOffSwitch.end(); iter++) {
		if(iter->bCmd == DISPLAY_UNIT) {
			sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), 
				D3DCOLOR_ARGB(255,255,255,255));
		}

		if(iter->bCmd == DISPLAY_ENV) {
			sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), 
				D3DCOLOR_ARGB(255,255,255,255));
		}

		if(iter->bCmd == DISPLAY_MISC) {
			sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), 
				D3DCOLOR_ARGB(255,255,255,255));
		}

		if(iter->bCmd == DISPLAY_MAP) {
			sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), 
				D3DCOLOR_ARGB(255,255,255,255));
		}
	}

	sprite->End();
}

void drawOpenMap(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/OpenMapBG.png", OPEN_MAP_BOX_WIDTH, OPEN_MAP_BOX_HEIGHT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	int x, y;
	x = (screenWidth/2) - (OPEN_MAP_BOX_WIDTH/2);
	y = (screenHeight/2) - (OPEN_MAP_BOX_HEIGHT/2);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)x,(float)y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	list<BUTTON>::iterator iter = OpenMapButtons.begin();
	for(;iter != OpenMapButtons.end(); iter++) {
		if(iter->bCmd != OPEN_MAP_ICON_BUTTON)
			sprite->Draw(iter->pic, NULL, NULL, &D3DXVECTOR3((float)iter->x,(float)iter->y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	}

	sprite->End();

	string fVal;
	char cVal[253];
	string tempString;

	list<BUTTON>::iterator iter2 = OpenMapButtons.begin();
	list<string>::iterator iter3 = OpenFilesList.begin();
	for(;iter2 != OpenMapButtons.end(); iter2++) {
		if(iter2->bCmd == OPEN_MAP_ICON_BUTTON) {
			fVal = *iter3;
			sprintf_s(cVal, sizeof(cVal), "%s", fVal);
			tempString = cVal;
			RECT font_rect = {iter2->x,iter2->y,0,0};
			standardFont->DrawText(NULL,				// Sprite
								iter3->c_str(),			// String
								-1,						// Count
								&font_rect,				// Rect
								FONT_FORMAT,			// Format
								FONT_COLOR);			// Color
			iter3++;
		}	
	}
}

void drawSaveMapAs(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/SaveMapAsBG.png", SAVE_AS_BOX_WIDTH, SAVE_AS_BOX_HEIGHT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	int x, y;
	x = (screenWidth/2) - (SAVE_AS_BOX_WIDTH/2);
	y = (screenHeight/2) - (SAVE_AS_BOX_HEIGHT/2);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)x,(float)y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	sprite->End();

	RECT font_rect = {x + 20,y + 60,0,0};
	standardFont->DrawText(NULL,						// Sprite
						inputSave,						// String
						-1,								// Count
						&font_rect,						// Rect
						FONT_FORMAT,					// Format
						FONT_COLOR);					// Color
}

void drawSavingMap(void) {
	sprite->Begin(D3DXSPRITE_ALPHABLEND);

	LPDIRECT3DTEXTURE9 bg;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/SavingMapMSG.png", SAVING_MAP_MSG_WIDTH, SAVING_MAP_MSG_HEIGHT, 
								D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, 0, NULL, NULL, &bg);
	int x, y;
	x = (screenWidth/2) - (SAVING_MAP_MSG_WIDTH/2);
	y = (screenHeight/2) - (SAVING_MAP_MSG_HEIGHT/2);
	sprite->Draw(bg, NULL, NULL, &D3DXVECTOR3((float)x,(float)y,(float)0), D3DCOLOR_ARGB(255,255,255,255));
	bg->Release();

	sprite->End();
}

void drawUnitInfoText(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);
	RECT font_rect;
	list<BUTTON>::iterator iter = UnitButtons.begin();
	
	float fVal;
	char cVal[32];
	string tempString;

	for(;iter != UnitButtons.end(); iter++) {
		if(iter->bCmd == PLUS_LOCATION_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getPosition().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LOCATION_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getPosition().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LOCATION_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getPosition().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getRotation().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getRotation().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getRotation().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getScale().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getScale().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedUnit->getScale().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LEVEL) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = (float)selectedUnit->getLevel();
			sprintf_s(cVal, sizeof(cVal), "%i", (int)fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ALIGNMENT) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedUnit->getAlignment().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_VALUE) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = (float)selectedUnit->getValue();
			sprintf_s(cVal, sizeof(cVal), "%i", (int)fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_CAN_MOVE) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedUnit->getCanMove().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_STANCE) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedUnit->getStance().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_GROUP) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = (float)selectedUnit->getGroup();
			sprintf_s(cVal, sizeof(cVal), "%i", (int)fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
	}
}

void drawEnvInfoText(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);
	RECT font_rect;
	list<BUTTON>::iterator iter = EnvButtons.begin();
	
	float fVal;
	char cVal[32];
	string tempString;

	for(;iter != EnvButtons.end(); iter++) {
		if(iter->bCmd == PLUS_LOCATION_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getPosition().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LOCATION_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getPosition().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LOCATION_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getPosition().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getRotation().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getRotation().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getRotation().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getScale().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getScale().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedEnv->getScale().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ALIGNMENT) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedEnv->getAlignment().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_VALUE) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = (float)selectedEnv->getValue();
			sprintf_s(cVal, sizeof(cVal), "%i", (int)fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
	}
}

void drawMiscInfoText(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);
	RECT font_rect;
	list<BUTTON>::iterator iter = MiscButtons.begin();
	
	float fVal;
	char cVal[32];
	string tempString;

	for(;iter != MiscButtons.end(); iter++) {
		if(iter->bCmd == PLUS_LOCATION_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getPosition().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LOCATION_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getPosition().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LOCATION_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getPosition().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getRotation().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getRotation().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ROTATION_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getRotation().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getScale().x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getScale().y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SCALE_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getScale().z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ALIGNMENT) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedMisc->getAlignment().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_PURPOSE) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedMisc->getPurpose().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_HRADIUS) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = selectedMisc->getHRadius();
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_ORDER) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = (float)selectedMisc->getOrder();
			sprintf_s(cVal, sizeof(cVal), "%i", (int)fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_GROUP) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = (float)selectedMisc->getGroup();
			sprintf_s(cVal, sizeof(cVal), "%i", (int)fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_SOUNDFILE) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			standardFont->DrawTextA(NULL,
									selectedMisc->getSoundFile().c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
	}
}

void drawLightInfoText(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);
	RECT font_rect;
	list<BUTTON>::iterator iter = LightButtons.begin();
	
	float fVal;
	char cVal[32];
	string tempString;

	for(;iter != LightButtons.end(); iter++) {
		if(iter->bCmd == PLUS_LIGHT_DIR_X) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			//fVal = light.Direction.x;
			fVal = lightDir.x;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_DIR_Y) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = lightDir.y;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_DIR_Z) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = lightDir.z;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}

		//-----------------------------------------------------------------
		if(iter->bCmd == PLUS_LIGHT_DIF_R) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Diffuse.r;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_DIF_G) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Diffuse.g;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_DIF_B) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Diffuse.b;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}

		//-----------------------------------------------------------------
		if(iter->bCmd == PLUS_LIGHT_SPE_R) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Specular.r;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_SPE_G) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Specular.g;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_SPE_B) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Specular.b;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		//-----------------------------------------------------------------
		if(iter->bCmd == PLUS_LIGHT_AMB_R) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Ambient.r;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_AMB_G) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Ambient.g;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
		if(iter->bCmd == PLUS_LIGHT_AMB_B) {
			font_rect.left = iter->x + (long)(SUBMENU_BUTTON_WIDTH) + 2;
			font_rect.top = iter->y + (int)(font.tmHeight/2);
			fVal = light.Ambient.b;
			sprintf_s(cVal, sizeof(cVal), "%.04f", fVal);
			tempString = cVal;
			standardFont->DrawTextA(NULL,
									tempString.c_str(),
									-1,
									&font_rect,
									FONT_FORMAT,
									FONT_COLOR);
		}
	}
}

void drawInfoText(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);
	RECT font_rect;
	
	int fVal;
	char cVal[32];
	string tempString;

	font_rect.left = 5;
	font_rect.top = MenuButtons.front().y + MenuButtons.front().h;
	fVal = UnitList.size();
	sprintf_s(cVal, sizeof(cVal), "Units: %i", fVal);
	tempString = cVal;
	standardFont->DrawTextA(NULL,
							tempString.c_str(),
							-1,
							&font_rect,
							FONT_FORMAT,
							FONT_COLOR);

	font_rect.left = 5;
	font_rect.top = font_rect.top + font.tmHeight;
	fVal = EnvList.size();
	sprintf_s(cVal, sizeof(cVal), "Env. Objects: %i", fVal);
	tempString = cVal;
	standardFont->DrawTextA(NULL,
							tempString.c_str(),
							-1,
							&font_rect,
							FONT_FORMAT,
							FONT_COLOR);
	
	font_rect.left = 5;
	font_rect.top = font_rect.top + font.tmHeight;
	fVal = MiscList.size();
	sprintf_s(cVal, sizeof(cVal), "Misc. Objects: %i", fVal);
	tempString = cVal;
	standardFont->DrawTextA(NULL,
							tempString.c_str(),
							-1,
							&font_rect,
							FONT_FORMAT,
							FONT_COLOR);
}

/*****************************
Input Functions
*****************************/
void handleInput(void) {
	POINT ptCursor;
	GetCursorPos(&ptCursor);
	ScreenToClient(wHandle, &ptCursor);
	mousePosition.x = ptCursor.x;
	mousePosition.y = ptCursor.y;

	//pointCamera(D3DXVECTOR3(INITIALIZE_CAMERA_MOVE.x,INITIALIZE_CAMERA_MOVE.y,INITIALIZE_CAMERA_MOVE.z+20));
	//_cprintf("Dir: [x]%.02f [y]%.02f [z]%.02f\n", );

	if(cState == UNIT_SELECTED) {
		if(dKey[DIK_H] & 0x80) {

		}
	}

	if(cState != SAVING_MAP_AS) {
		if(dKey[DIK_T] & 0x80) {
			_cprintf("UnitList Size: %i\n", UnitList.size());
		}
	}

	if(cState == SAVING_MAP_AS) {
		if(now2 <= then2) {
			for(int i=0; i<237; i++) {
				dKey[i] = 0x00;
			}
		} else {
			inputSaveMapAs();
			then2 = now2 + POLL_TIME2;
		}
	}

	if(cState != SAVING_MAP_AS) {
		if(dKey[DIK_A] & 0x80) {
			position.x -= CAMERA_MOVE_SPEED;
			lookPos.x -= CAMERA_MOVE_SPEED;
			moveCamera(position);
			pointCamera(lookPos);
		}
		if(dKey[DIK_D] & 0x80) {
			position.x += CAMERA_MOVE_SPEED;
			lookPos.x += CAMERA_MOVE_SPEED;
			moveCamera(position);
			pointCamera(lookPos);
		}
		if(dKey[DIK_W] & 0x80) {
			position.z += CAMERA_MOVE_SPEED;
			lookPos.z += CAMERA_MOVE_SPEED;
			moveCamera(position);
			pointCamera(lookPos);
		}
		if(dKey[DIK_S] & 0x80) {
			position.z -= CAMERA_MOVE_SPEED;
			lookPos.z -= CAMERA_MOVE_SPEED;
			moveCamera(position);
			pointCamera(lookPos);
		}
		if(dKey[DIK_1] & 0x80)
			pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
		if(dKey[DIK_2] & 0x80)
			pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
		if(dKey[DIK_3] & 0x80)
			pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_POINT);
		
		if(dKey[DIK_I] & 0x80)
			MapObject->scaleObject(0.0f,0.0f,0.1f);
		if(dKey[DIK_K] & 0x80)
			MapObject->scaleObject(0.0f,0.0f,-0.1f);
		if(dKey[DIK_J] & 0x80)
			MapObject->scaleObject(0.1f,0.0f,0.0f);
		if(dKey[DIK_L] & 0x80)
			MapObject->scaleObject(-0.1f,0.0f,0.0f);
	}

	if(dKey[DIK_ESCAPE] & 0x80) {
		//PostMessage(wHandle, WM_DESTROY, 0, 0);
		moveCamera(INITIALIZE_CAMERA_MOVE);
		pointCamera(INITIALIZE_CAMERA_POINT);
		cState = CLEAR;
	}

	if(mouseState.lZ > 0) {
		position.y -= CAMERA_ZOOM_SPEED;
		moveCamera(position);
		pointCamera(lookPos);
	}
	if(mouseState.lZ < 0) {
		position.y += CAMERA_ZOOM_SPEED;
		moveCamera(position);
		pointCamera(lookPos);
	}

	if(mouseState.rgbButtons[0] & 0x80) {
		// left mouse button throttle code
		if(now <= then) {
			if(mousePosition.x >= prevMousePosition.x - 1 && mousePosition.y <= prevMousePosition.x + 1) {
				if(mousePosition.y >= prevMousePosition.y - 1 && mousePosition.y <= prevMousePosition.y + 1) {
					mouseState.rgbButtons[0] = 0x00; // reset left mouse button state
				}
			}
		} else {
			handleLeftMousePress(); // handle the left mouse button presses
			prevMousePosition.x = mousePosition.x;
			prevMousePosition.y = mousePosition.y;
			then = now + POLL_TIME;
		}
	}
	if(mouseState.rgbButtons[1] & 0x80) {
		handleRightMousePress();
	}
}

void handleLeftMousePress(void) {
	bool buttonPressed = false;
	// Top-Menu Buttons
	if(mousePosition.y <= (UnitButtons.back().y + UnitButtons.back().h)) {
		list<BUTTON>::iterator iter = MenuButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != MenuButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				topMenuButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == UNIT_SELECTED) {
		list<BUTTON>::iterator iter = UnitButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != UnitButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				unitButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == ENV_SELECTED) {
		list<BUTTON>::iterator iter = EnvButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != EnvButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				envButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == MISC_SELECTED) {
		list<BUTTON>::iterator iter = MiscButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != MiscButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				miscButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == LIGHT_MENU) {
		list<BUTTON>::iterator iter = LightButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != LightButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				lightButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == UNITS_MENU) {
		list<BUTTON>::iterator iter = UnitSideListButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != UnitSideListButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				unitModelButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == ENV_MENU) {
		list<BUTTON>::iterator iter = EnvSideListButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != EnvSideListButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				envModelButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == MISC_MENU) {
		list<BUTTON>::iterator iter = MiscSideListButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != MiscSideListButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				miscModelButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState != UNIT_SELECTED && cState != ENV_SELECTED && cState != MISC_SELECTED) {
		list<BUTTON>::iterator iter = DisplayOnOffSwitch.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != DisplayOnOffSwitch.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
				displayOnOffSwitchButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == OPENING_MAP) {
		list<BUTTON>::iterator iter = OpenMapButtons.begin();
		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT rectangle;
		for(;iter != OpenMapButtons.end(); iter++) {
			rectangle.left = iter->x;
			rectangle.right = iter->x + iter->w;
			rectangle.top = iter->y;
			rectangle.bottom = iter->y + iter->h;

			if(isPointInRect(pair, rectangle)) {
				buttonPressed = true;
			openMapButtonFunctions(iter->bCmd);
			}
		}
	}

	if(cState == UNIT_SELECTED || cState == ENV_SELECTED || cState == MISC_SELECTED) {
		list<BUTTON>::iterator iter = UnitSideListButtons.begin();
		for(;iter != UnitSideListButtons.end(); iter++) {
			if(iter->bCmd == MODEL_ICON_BUTTON)
				break;
		}

		INTPair pair = {mousePosition.x, mousePosition.y};
		RECT tempRect;
		tempRect.left = iter->x;
		tempRect.top = iter->y;
		tempRect.right = iter->x + MODEL_ICON_WIDTH;
		tempRect.bottom = iter->y + (MODEL_ICONS_VISIBLE * iter->h);

		if(isPointInRect(pair, tempRect)) {
			buttonPressed = true;
		}
		if(mousePosition.y >= UnitButtons.back().y) {
			buttonPressed = true;
		}
	}

	if(mousePosition.y >= UnitButtons.back().y)
		buttonPressed = true;

	if(!buttonPressed) {
		if(mousePicking()) {
		} else {
			if(cState != SAVING_MAP && cState != SAVING_MAP_AS)
				cState = CLEAR;
		}
	}
}

void handleRightMousePress(void) {
	if(!mousePicking2()) {
		if(cState == UNIT_SELECTED) {
			UnitList.push_front(*selectedUnit);
			cState = UNITS_MENU;
		}
		if(cState == ENV_SELECTED) {
			EnvList.push_front(*selectedEnv);
			cState = ENV_MENU;
		}
		if(cState == MISC_SELECTED) {
			MiscList.push_front(*selectedMisc);
			cState = MISC_MENU;
		}
	}
}

/*****************************
Button Command Functions
*****************************/
void topMenuButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == NEW_MAP) {
		// erase all object lists and reset camera and state
		eraseLists();
	}
	if(cmd == OPEN_MAP) {
		// clear all and open a map
		setOpenFilesList();
		resetOpenFileButtons();
		cState = OPENING_MAP;
	}
	int iter = 0;
	if(cmd == SAVE_MAP) {
		cState = SAVING_MAP;
	}
	if(cmd == SAVE_MAP_AS) {
		for(int i=0; i<inputSaveElement; i++) {
			if(inputSave[i] == '.') {
				inputSave[i] = NULL;
				inputSave[++i] = NULL;
				inputSave[++i] = NULL;
				inputSaveElement -= 3;
				break;
			}
		}
		cState = SAVING_MAP_AS;
	}
	if(cmd == UNIT_OBJECTS) {
		cState = UNITS_MENU;
	}
	if(cmd == ENV_OBJECTS) {
		cState = ENV_MENU;
	}
	if(cmd == MISC_OBJECTS) {
		cState = MISC_MENU;
	}
	if(cmd == LIGHT_OBJECT) {
		cState = LIGHT_MENU;
	}
}

void unitButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == PLACE_OBJECT) {
		UnitList.push_front(*selectedUnit);
		cState = UNITS_MENU;
	}
	if(cmd == DELETE_OBJECT) {
		cState = UNITS_MENU;
	}
	if(cmd == GROUND_Y) {
		selectedUnit->setObjectLocation(selectedUnit->getPosition().x, (float)INITIAL_OBJECT_HEIGHT, selectedUnit->getPosition().z);
	}
	if(cmd == PLAYER_Y) {
		bool found = false;
		list<Misc>::iterator iter = MiscList.begin();
		for(;iter != MiscList.end(); iter++) {
			if(iter->getPurpose() == "PlayerStart") {
				selectedUnit->setObjectLocation(selectedUnit->getPosition().x, iter->getPosition().y, selectedUnit->getPosition().z);
				found = true;
				break;
			}
		}
		list<Unit>::iterator iter2 = UnitList.begin();
		for(;iter2 != UnitList.end(); iter2++) {
			if(iter2->getAlignment() == "Player") {
				selectedUnit->setObjectLocation(selectedUnit->getPosition().x, iter2->getPosition().y, selectedUnit->getPosition().z);
				found = true;
				break;
			}
		}
		if(!found)
			selectedUnit->setObjectLocation(selectedUnit->getPosition().x, STANDARD_PLAYER_Y, selectedUnit->getPosition().z);
	}

	if(cmd == PLUS_LOCATION_X) {
		selectedUnit->moveObject((float)LOCATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_LOCATION_Y) {
		selectedUnit->moveObject(0.0f, (float)LOCATION_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_LOCATION_Z) {
		selectedUnit->moveObject(0.0f, 0.0f, (float)LOCATION_Z_CHANGE);
	}
	if(cmd == MINUS_LOCATION_X) {
		selectedUnit->moveObject((float)-LOCATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_LOCATION_Y) {
		selectedUnit->moveObject(0.0f, (float)-LOCATION_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_LOCATION_Z) {
		selectedUnit->moveObject(0.0f, 0.0f, (float)-LOCATION_Z_CHANGE);
	}

	if(cmd == PLUS_ROTATION_X) {
		selectedUnit->rotateObject((float)ROTATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_ROTATION_Y) {
		selectedUnit->rotateObject(0.0f, (float)ROTATION_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_ROTATION_Z) {
		selectedUnit->rotateObject(0.0f, 0.0f, (float)ROTATION_Z_CHANGE);
	}
	if(cmd == MINUS_ROTATION_X) {
		selectedUnit->rotateObject((float)-ROTATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_ROTATION_Y) {
		selectedUnit->rotateObject(0.0f, (float)-ROTATION_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_ROTATION_Z) {
		selectedUnit->rotateObject(0.0f, 0.0f, (float)-ROTATION_Z_CHANGE);
	}

	if(cmd == PLUS_SCALE_X) {
		selectedUnit->scaleObject((float)SCALE_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_SCALE_Y) {
		selectedUnit->scaleObject(0.0f, (float)SCALE_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_SCALE_Z) {
		selectedUnit->scaleObject(0.0f, 0.0f, (float)SCALE_Z_CHANGE);
	}
	if(cmd == MINUS_SCALE_X) {
		selectedUnit->scaleObject((float)-SCALE_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_SCALE_Y) {
		selectedUnit->scaleObject(0.0f, (float)-SCALE_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_SCALE_Z) {
		selectedUnit->scaleObject(0.0f, 0.0f, (float)-SCALE_Z_CHANGE);
	}

	if(cmd == PLUS_ALIGNMENT) {
		AlignmentList.push_back(AlignmentList.front());
		AlignmentList.pop_front();
		selectedUnit->setAlignment(AlignmentList.front());
	}
	if(cmd == MINUS_ALIGNMENT) {
		AlignmentList.push_front(AlignmentList.back());
		AlignmentList.pop_back();
		selectedUnit->setAlignment(AlignmentList.front());
	}
	if(cmd == PLUS_LEVEL) {
		selectedUnit->setLevel(selectedUnit->getLevel() + 1);
	}
	if(cmd == MINUS_LEVEL) {
		selectedUnit->setLevel(selectedUnit->getLevel() - 1);
	}
	if(cmd == PLUS_VALUE) {
		selectedUnit->setValue(selectedUnit->getValue() + 1);
	}
	if(cmd == MINUS_VALUE) {
		selectedUnit->setValue(selectedUnit->getValue() - 1);
	}
	if(cmd == PLUS_CAN_MOVE) {
		CanMoveList.push_back(CanMoveList.front());
		CanMoveList.pop_front();
		selectedUnit->setCanMove(CanMoveList.front());
	}
	if(cmd == MINUS_CAN_MOVE) {
		CanMoveList.push_front(CanMoveList.back());
		CanMoveList.pop_back();
		selectedUnit->setCanMove(CanMoveList.front());
	}
	if(cmd == PLUS_STANCE) {
		StanceList.push_back(StanceList.front());
		StanceList.pop_front();
		selectedUnit->setStance(StanceList.front());
	}
	if(cmd == MINUS_STANCE) {
		StanceList.push_front(StanceList.back());
		StanceList.pop_back();
		selectedUnit->setStance(StanceList.front());
	}
	if(cmd == PLUS_GROUP) {
		selectedUnit->setGroup(selectedUnit->getGroup() + 1);
	}
	if(cmd == MINUS_GROUP) {
		selectedUnit->setGroup(selectedUnit->getGroup() - 1);
	}
}

void envButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == PLACE_OBJECT) {
		EnvList.push_front(*selectedEnv);
		cState = ENV_MENU;
	}
	if(cmd == DELETE_OBJECT) {
		cState = ENV_MENU;
	}
	if(cmd == GROUND_Y) {
		selectedEnv->setObjectLocation(selectedEnv->getPosition().x, (float)INITIAL_OBJECT_HEIGHT, selectedEnv->getPosition().z);
	}
	if(cmd == PLAYER_Y) {
		bool found = false;
		list<Misc>::iterator iter = MiscList.begin();
		for(;iter != MiscList.end(); iter++) {
			if(iter->getPurpose() == "PlayerStart") {
				selectedEnv->setObjectLocation(selectedEnv->getPosition().x, iter->getPosition().y, selectedEnv->getPosition().z);
				found = true;
				break;
			}
		}
		list<Unit>::iterator iter2 = UnitList.begin();
		for(;iter2 != UnitList.end(); iter2++) {
			if(iter2->getAlignment() == "Player") {
				selectedUnit->setObjectLocation(selectedUnit->getPosition().x, iter2->getPosition().y, selectedUnit->getPosition().z);
				found = true;
				break;
			}
		}
		if(!found)
			selectedEnv->setObjectLocation(selectedEnv->getPosition().x, STANDARD_PLAYER_Y, selectedEnv->getPosition().z);
	}

	if(cmd == PLUS_LOCATION_X) {
		selectedEnv->moveObject((float)LOCATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_LOCATION_Y) {
		selectedEnv->moveObject(0.0f, (float)LOCATION_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_LOCATION_Z) {
		selectedEnv->moveObject(0.0f, 0.0f, (float)LOCATION_Z_CHANGE);
	}
	if(cmd == MINUS_LOCATION_X) {
		selectedEnv->moveObject((float)-LOCATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_LOCATION_Y) {
		selectedEnv->moveObject(0.0f, (float)-LOCATION_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_LOCATION_Z) {
		selectedEnv->moveObject(0.0f, 0.0f, (float)-LOCATION_Z_CHANGE);
	}

	if(cmd == PLUS_ROTATION_X) {
		selectedEnv->rotateObject((float)ROTATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_ROTATION_Y) {
		selectedEnv->rotateObject(0.0f, (float)ROTATION_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_ROTATION_Z) {
		selectedEnv->rotateObject(0.0f, 0.0f, (float)ROTATION_Z_CHANGE);
	}
	if(cmd == MINUS_ROTATION_X) {
		selectedEnv->rotateObject((float)-ROTATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_ROTATION_Y) {
		selectedEnv->rotateObject(0.0f, (float)-ROTATION_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_ROTATION_Z) {
		selectedEnv->rotateObject(0.0f, 0.0f, (float)-ROTATION_Z_CHANGE);
	}

	if(cmd == PLUS_SCALE_X) {
		selectedEnv->scaleObject((float)SCALE_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_SCALE_Y) {
		selectedEnv->scaleObject(0.0f, (float)SCALE_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_SCALE_Z) {
		selectedEnv->scaleObject(0.0f, 0.0f, (float)SCALE_Z_CHANGE);
	}
	if(cmd == MINUS_SCALE_X) {
		selectedEnv->scaleObject((float)-SCALE_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_SCALE_Y) {
		selectedEnv->scaleObject(0.0f, (float)-SCALE_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_SCALE_Z) {
		selectedEnv->scaleObject(0.0f, 0.0f, (float)-SCALE_Z_CHANGE);
	}

	if(cmd == PLUS_ALIGNMENT) {
		AlignmentList.push_back(AlignmentList.front());
		AlignmentList.pop_front();
		selectedEnv->setAlignment(AlignmentList.front());
	}
	if(cmd == MINUS_ALIGNMENT) {
		AlignmentList.push_front(AlignmentList.back());
		AlignmentList.pop_back();
		selectedEnv->setAlignment(AlignmentList.front());
	}
	if(cmd == PLUS_VALUE) {
		selectedEnv->setValue(selectedEnv->getValue() + 1);
	}
	if(cmd == MINUS_VALUE) {
		selectedEnv->setValue(selectedEnv->getValue() - 1);
	}
}

void miscButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == PLACE_OBJECT) {
		MiscList.push_front(*selectedMisc);
		cState = MISC_MENU;
	}
	if(cmd == DELETE_OBJECT) {
		cState = MISC_MENU;
	}
	if(cmd == GROUND_Y) {
		selectedMisc->setObjectLocation(selectedMisc->getPosition().x, (float)INITIAL_OBJECT_HEIGHT, selectedMisc->getPosition().z);
	}
	if(cmd == PLAYER_Y) {
		bool found = false;
		list<Misc>::iterator iter = MiscList.begin();
		for(;iter != MiscList.end(); iter++) {
			if(iter->getPurpose() == "PlayerStart") {
				selectedMisc->setObjectLocation(selectedMisc->getPosition().x, iter->getPosition().y, selectedMisc->getPosition().z);
				found = true;
				break;
			}
		}
		list<Unit>::iterator iter2 = UnitList.begin();
		for(;iter2 != UnitList.end(); iter2++) {
			if(iter2->getAlignment() == "Player") {
				selectedUnit->setObjectLocation(selectedUnit->getPosition().x, iter2->getPosition().y, selectedUnit->getPosition().z);
				found = true;
				break;
			}
		}
		if(!found)
			selectedMisc->setObjectLocation(selectedMisc->getPosition().x, STANDARD_PLAYER_Y, selectedMisc->getPosition().z);
	}

	if(cmd == PLUS_LOCATION_X) {
		selectedMisc->moveObject((float)LOCATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_LOCATION_Y) {
		selectedMisc->moveObject(0.0f, (float)LOCATION_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_LOCATION_Z) {
		selectedMisc->moveObject(0.0f, 0.0f, (float)LOCATION_Z_CHANGE);
	}
	if(cmd == MINUS_LOCATION_X) {
		selectedMisc->moveObject((float)-LOCATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_LOCATION_Y) {
		selectedMisc->moveObject(0.0f, (float)-LOCATION_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_LOCATION_Z) {
		selectedMisc->moveObject(0.0f, 0.0f, (float)-LOCATION_Z_CHANGE);
	}

	if(cmd == PLUS_ROTATION_X) {
		selectedMisc->rotateObject((float)ROTATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_ROTATION_Y) {
		selectedMisc->rotateObject(0.0f, (float)ROTATION_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_ROTATION_Z) {
		selectedMisc->rotateObject(0.0f, 0.0f, (float)ROTATION_Z_CHANGE);
	}
	if(cmd == MINUS_ROTATION_X) {
		selectedMisc->rotateObject((float)-ROTATION_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_ROTATION_Y) {
		selectedMisc->rotateObject(0.0f, (float)-ROTATION_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_ROTATION_Z) {
		selectedMisc->rotateObject(0.0f, 0.0f, (float)-ROTATION_Z_CHANGE);
	}

	if(cmd == PLUS_SCALE_X) {
		selectedMisc->scaleObject((float)SCALE_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == PLUS_SCALE_Y) {
		selectedMisc->scaleObject(0.0f, (float)SCALE_Y_CHANGE, 0.0f);
	}
	if(cmd == PLUS_SCALE_Z) {
		selectedMisc->scaleObject(0.0f, 0.0f, (float)SCALE_Z_CHANGE);
	}
	if(cmd == MINUS_SCALE_X) {
		selectedMisc->scaleObject((float)-SCALE_X_CHANGE, 0.0f, 0.0f);
	}
	if(cmd == MINUS_SCALE_Y) {
		selectedMisc->scaleObject(0.0f, (float)-SCALE_Y_CHANGE, 0.0f);
	}
	if(cmd == MINUS_SCALE_Z) {
		selectedMisc->scaleObject(0.0f, 0.0f, (float)-SCALE_Z_CHANGE);
	}

	if(cmd == PLUS_ALIGNMENT) {
		AlignmentList.push_back(AlignmentList.front());
		AlignmentList.pop_front();
		selectedMisc->setAlignment(AlignmentList.front());
	}
	if(cmd == MINUS_ALIGNMENT) {
		AlignmentList.push_front(AlignmentList.back());
		AlignmentList.pop_back();
		selectedMisc->setAlignment(AlignmentList.front());
	}
	if(cmd == PLUS_PURPOSE) {
		PurposeList.push_back(PurposeList.front());
		PurposeList.pop_front();
		selectedMisc->setPurpose(PurposeList.front());
	}
	if(cmd == MINUS_PURPOSE) {
		PurposeList.push_front(PurposeList.back());
		PurposeList.pop_back();
		selectedMisc->setPurpose(PurposeList.front());
	}
	if(cmd == PLUS_HRADIUS) {
		selectedMisc->setHRadius(selectedMisc->getHRadius() + 1);
	}
	if(cmd == MINUS_HRADIUS) {
		selectedMisc->setHRadius(selectedMisc->getHRadius() - 1);
	}
	if(cmd == PLUS_ORDER) {
		selectedMisc->setOrder(selectedMisc->getOrder() + 1);
	}
	if(cmd == MINUS_ORDER) {
		selectedMisc->setOrder(selectedMisc->getOrder() - 1);
	}
	if(cmd == PLUS_GROUP) {
		selectedMisc->setGroup(selectedMisc->getGroup() + 1);
	}
	if(cmd == MINUS_GROUP) {
		selectedMisc->setGroup(selectedMisc->getGroup() - 1);
	}
	if(cmd == PLUS_SOUNDFILE) {
		SoundFilesList.push_back(SoundFilesList.front());
		SoundFilesList.pop_front();
		selectedMisc->setSoundFile(SoundFilesList.front());
	}
	if(cmd == MINUS_SOUNDFILE) {
		SoundFilesList.push_front(SoundFilesList.back());
		SoundFilesList.pop_back();
		selectedMisc->setSoundFile(SoundFilesList.front());
	}
}

void lightButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == PLUS_LIGHT_DIR_X) {
		lightDir.x += (float)LIGHT_DIRECTION_CHANGE_SPEED;
		D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	}
	if(cmd == PLUS_LIGHT_DIR_Y) {
		lightDir.y += (float)LIGHT_DIRECTION_CHANGE_SPEED;
		D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	}
	if(cmd == PLUS_LIGHT_DIR_Z) {
		lightDir.z += (float)LIGHT_DIRECTION_CHANGE_SPEED;
		D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	}
	if(cmd == MINUS_LIGHT_DIR_X) {
		lightDir.x -= (float)LIGHT_DIRECTION_CHANGE_SPEED;
		D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	}
	if(cmd == MINUS_LIGHT_DIR_Y) {
		lightDir.y -= (float)LIGHT_DIRECTION_CHANGE_SPEED;
		D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	}
	if(cmd == MINUS_LIGHT_DIR_Z) {
		lightDir.z -= (float)LIGHT_DIRECTION_CHANGE_SPEED;
		D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	}

	if(cmd == PLUS_LIGHT_DIF_R) {
		light.Diffuse.r += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == PLUS_LIGHT_DIF_G) {
		light.Diffuse.g += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == PLUS_LIGHT_DIF_B) {
		light.Diffuse.b += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_DIF_R) {
		light.Diffuse.r -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_DIF_G) {
		light.Diffuse.g -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_DIF_B) {
		light.Diffuse.b -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	
	if(cmd == PLUS_LIGHT_SPE_R) {
		light.Specular.r += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == PLUS_LIGHT_SPE_G) {
		light.Specular.g += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == PLUS_LIGHT_SPE_B) {
		light.Specular.b += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_SPE_R) {
		light.Specular.r -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_SPE_G) {
		light.Specular.g -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_SPE_B) {
		light.Specular.b -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}

	if(cmd == PLUS_LIGHT_AMB_R) {
		light.Ambient.r += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == PLUS_LIGHT_AMB_G) {
		light.Ambient.g += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == PLUS_LIGHT_AMB_B) {
		light.Ambient.b += (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_AMB_R) {
		light.Ambient.r -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_AMB_G) {
		light.Ambient.g -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}
	if(cmd == MINUS_LIGHT_AMB_B) {
		light.Ambient.b -= (float)LIGHT_COLOR_CHANGE_SPEED;
	}

	if(cmd == RESET_LIGHT) {
		initializeLight();
	}
	pd3dDevice->SetLight(0, &light);
}

void unitModelButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == SCROLL_UP) {
		UnitObjects.push_back(UnitObjects.front());
		UnitObjects.pop_front();

		list<BUTTON>::iterator iter1 = UnitSideListButtons.begin();
		list<Unit>::iterator iter2 = UnitObjects.begin();
		for(;iter1 != UnitSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				D3DXCreateTextureFromFileEx(pd3dDevice, iter2->getIconLocation().c_str(), iter1->w, iter1->h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter1->pic);
				iter2++;
			}
		}
	}
	if(cmd == SCROLL_DOWN) {
		UnitObjects.push_front(UnitObjects.back());
		UnitObjects.pop_back();

		list<BUTTON>::iterator iter1 = UnitSideListButtons.begin();
		list<Unit>::iterator iter2 = UnitObjects.begin();
		for(;iter1 != UnitSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				D3DXCreateTextureFromFileEx(pd3dDevice, iter2->getIconLocation().c_str(), iter1->w, iter1->h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter1->pic);
				iter2++;
			}
		}
	}
	if(cmd == MODEL_ICON_BUTTON) {
		INTPair mouseCoords = {mousePosition.x, mousePosition.y};
		RECT buttonRect;
		int accumulator = 0;

		list<BUTTON>::iterator iter1 = UnitSideListButtons.begin();
		for(;iter1 != UnitSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				accumulator++;
				buttonRect.left = iter1->x;
				buttonRect.right = iter1->x + iter1->w;
				buttonRect.top = iter1->y;
				buttonRect.bottom = iter1->y + iter1->h;
				
				if(isPointInRect(mouseCoords, buttonRect)) {
					break;
				}
			}
		}
		list<Unit>::iterator iter2 = UnitObjects.begin();
		for(int i=1; i != accumulator; i++) {
			iter2++;
		}

		delete selectedUnit;
		
		selectedUnit = new Unit(iter2->getModelFile(), iter2->getDataFile(), iter2->getIconLocation(),
			iter2->getName(), iter2->getAlignment(), iter2->getTMatrix(), iter2->getValue(), 
			iter2->getCanMove(), iter2->getStance());
		selectedUnit->initializeObject(pd3dDevice);
		selectedUnit->moveObject(lookPos.x, INITIAL_OBJECT_HEIGHT, lookPos.z);

		cState = UNIT_SELECTED;
	}
}

void envModelButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == SCROLL_UP) {
		EnvObjects.push_back(EnvObjects.front());
		EnvObjects.pop_front();

		list<BUTTON>::iterator iter1 = EnvSideListButtons.begin();
		list<Env>::iterator iter2 = EnvObjects.begin();
		for(;iter1 != EnvSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				D3DXCreateTextureFromFileEx(pd3dDevice, iter2->getIconLocation().c_str(), iter1->w, iter1->h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter1->pic);
				iter2++;
			}
		}
	}
	if(cmd == SCROLL_DOWN) {
		EnvObjects.push_front(EnvObjects.back());
		EnvObjects.pop_back();

		list<BUTTON>::iterator iter1 = EnvSideListButtons.begin();
		list<Env>::iterator iter2 = EnvObjects.begin();
		for(;iter1 != EnvSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				D3DXCreateTextureFromFileEx(pd3dDevice, iter2->getIconLocation().c_str(), iter1->w, iter1->h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter1->pic);
				iter2++;
			}
		}
	}
	if(cmd == MODEL_ICON_BUTTON) {
		INTPair mouseCoords = {mousePosition.x, mousePosition.y};
		RECT buttonRect;
		int accumulator = 0;

		list<BUTTON>::iterator iter1 = EnvSideListButtons.begin();
		for(;iter1 != EnvSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				accumulator++;
				buttonRect.left = iter1->x;
				buttonRect.right = iter1->x + iter1->w;
				buttonRect.top = iter1->y;
				buttonRect.bottom = iter1->y + iter1->h;
				
				if(isPointInRect(mouseCoords, buttonRect)) {
					break;
				}
			}
		}
		list<Env>::iterator iter2 = EnvObjects.begin();
		for(int i=1; i != accumulator; i++) {
			iter2++;
		}

		delete selectedEnv;
		selectedEnv = new Env(iter2->getModelFile(), iter2->getDataFile(), iter2->getIconLocation(),
			iter2->getName(), iter2->getAlignment(), iter2->getTMatrix(), iter2->getValue());
		selectedEnv->initializeObject(pd3dDevice);
		selectedEnv->moveObject(lookPos.x, INITIAL_OBJECT_HEIGHT, lookPos.z);

		cState = ENV_SELECTED;
	}
}

void miscModelButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == SCROLL_UP) {
		MiscObjects.push_back(MiscObjects.front());
		MiscObjects.pop_front();

		list<BUTTON>::iterator iter1 = MiscSideListButtons.begin();
		list<Misc>::iterator iter2 = MiscObjects.begin();
		for(;iter1 != MiscSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				D3DXCreateTextureFromFileEx(pd3dDevice, iter2->getIconLocation().c_str(), iter1->w, iter1->h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter1->pic);
				iter2++;
			}
		}
	}
	if(cmd == SCROLL_DOWN) {
		MiscObjects.push_front(MiscObjects.back());
		MiscObjects.pop_back();

		list<BUTTON>::iterator iter1 = MiscSideListButtons.begin();
		list<Misc>::iterator iter2 = MiscObjects.begin();
		for(;iter1 != MiscSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				D3DXCreateTextureFromFileEx(pd3dDevice, iter2->getIconLocation().c_str(), iter1->w, iter1->h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter1->pic);
				iter2++;
			}
		}
	}
	if(cmd == MODEL_ICON_BUTTON) {
		INTPair mouseCoords = {mousePosition.x, mousePosition.y};
		RECT buttonRect;
		int accumulator = 0;

		list<BUTTON>::iterator iter1 = MiscSideListButtons.begin();
		for(;iter1 != MiscSideListButtons.end(); iter1++) {
			if(iter1->bCmd == MODEL_ICON_BUTTON) {
				accumulator++;
				buttonRect.left = iter1->x;
				buttonRect.right = iter1->x + iter1->w;
				buttonRect.top = iter1->y;
				buttonRect.bottom = iter1->y + iter1->h;
				
				if(isPointInRect(mouseCoords, buttonRect)) {
					break;
				}
			}
		}
		list<Misc>::iterator iter2 = MiscObjects.begin();
		for(int i=1; i != accumulator; i++)
			iter2++;

		delete selectedMisc;
		selectedMisc = new Misc(iter2->getModelFile(), iter2->getDataFile(), iter2->getIconLocation(),
			iter2->getName(), iter2->getAlignment(), iter2->getTMatrix(), iter2->getPurpose(), iter2->getHRadius(), 
			iter2->getOrder(), iter2->getType());
		selectedMisc->initializeObject(pd3dDevice);
		selectedMisc->moveObject(lookPos.x, INITIAL_OBJECT_HEIGHT, lookPos.z);

		cState = MISC_SELECTED;
	}
}

void displayOnOffSwitchButtonFunctions(BUTTON_CMD cmd) {
	list<BUTTON>::iterator iter = DisplayOnOffSwitch.begin();
	for(;iter != DisplayOnOffSwitch.end(); iter++) {
		if(cmd == DISPLAY_UNIT && iter->bCmd == cmd) {
			if(displayUnitList) {
				displayUnitList = false;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayUnitOffSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			} else {
				displayUnitList = true;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayUnitOnSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			}
		}
		if(cmd == DISPLAY_ENV && iter->bCmd == cmd) {
			if(displayEnvList) {
				displayEnvList = false;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayEnvOffSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			} else {
				displayEnvList = true;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayEnvOnSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			}
		}
		if(cmd == DISPLAY_MISC && iter->bCmd == cmd) {
			if(displayMiscList) {
				displayMiscList = false;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayMiscOffSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			} else {
				displayMiscList = true;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayMiscOnSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			}
		}
		if(cmd == DISPLAY_MAP && iter->bCmd == cmd) {
			if(displayMap) {
				displayMap = false;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayMapOffSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			} else {
				displayMap = true;
				iter->pic->Release();
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/DisplayMapOnSwitch.png", iter->w, iter->h, 
					D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
					D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &iter->pic);
			}
		}
	}
}

void openMapButtonFunctions(BUTTON_CMD cmd) {
	if(cmd == SCROLL_UP) {
		if(!OpenFilesList.empty()) {
			OpenFilesList.push_back(OpenFilesList.front());
			OpenFilesList.pop_front();
		}
	}
	if(cmd == SCROLL_DOWN) {
		if(!OpenFilesList.empty()) {
			OpenFilesList.push_front(OpenFilesList.back());
			OpenFilesList.pop_back();
		}
	}
	if(cmd == OPEN_MAP_ICON_BUTTON) {
		INTPair mouseCoords = {mousePosition.x, mousePosition.y};
		RECT buttonRect;
		int accumulator = 0;

		list<BUTTON>::iterator iter1 = OpenMapButtons.begin();
		for(;iter1 != OpenMapButtons.end(); iter1++) {
			if(iter1->bCmd == OPEN_MAP_ICON_BUTTON) {
				accumulator++;
				buttonRect.left = iter1->x;
				buttonRect.right = iter1->x + iter1->w;
				buttonRect.top = iter1->y;
				buttonRect.bottom = iter1->y + iter1->h;
				
				if(isPointInRect(mouseCoords, buttonRect)) {
					break;
				}
			}
		}
		list<string>::iterator iter2 = OpenFilesList.begin();
		for(int i=1; i != accumulator; i++)
			iter2++;

		for(int i=0; i<FILE_NAME_LENGTH_MAX; i++)
			inputSave[i] = NULL;

		inputSaveElement = 0;
		for(int i=0; i<(signed)iter2->size(); i++) {
			inputSave[i] = (*iter2)[i];
			inputSaveElement++;
		}
		inputSave[inputSaveElement+1] = NULL;

		// reset current map and load new map
		openMap(*iter2);

		// resetting light, camera, and state
		moveCamera(INITIALIZE_CAMERA_MOVE);
		pointCamera(INITIALIZE_CAMERA_POINT);
		//initializeLight();
		//pd3dDevice->SetLight(0, &light);

		cState = CLEAR;
	}
}

/*****************************
Camera Functions
*****************************/
void createCamera(float nearClip, float farClip) {
	// specify the field of view, aspect ration and near and far clipping planes
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4.0f, (float)screenWidth/screenHeight, nearClip, farClip);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
void moveCamera(D3DXVECTOR3 vec) {
	position = vec;
}
void pointCamera(D3DXVECTOR3 vec) {
	lookPos = vec;

	D3DXMatrixLookAtLH(&matView, &position,								// Camera Position
                                 &lookPos,								// Look At Position
                                 &D3DXVECTOR3(0.0f, 1.0f, 0.0f));		// Up Direction

	pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
}

/*****************************
Initialize Light
*****************************/
void initializeLight(void) {
	lightDir			= INITIAL_LIGHT_DIRECTION;
	light.Type			= D3DLIGHT_DIRECTIONAL;
	light.Diffuse.r		= light.Diffuse.g  = light.Diffuse.b  = 1.0f;
	light.Specular.r	= light.Specular.g = light.Specular.b = 0.1f;
	light.Ambient.r		= light.Ambient.g  = light.Ambient.b  = 0.0f;
	D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
	light.Range			= 10000.0f;
	pd3dDevice->SetLight(0, &light);
	pd3dDevice->LightEnable(0, TRUE);
}

/*****************************
Misc. Functions
*****************************/
bool isPointInRect(INTPair point, RECT thisRect) {
	if(point.x >= thisRect.left && point.x <= thisRect.right) {
		if(point.y >= thisRect.top && point.y <= thisRect.bottom) {
			return true;
		}
	}
	return false;
}
bool mousePicking(void) {
	D3DXVECTOR3 v;
	v.x =  ( ( ( 2.0f * mousePosition.x ) / screenWidth  ) - 1 ) / matProj._11;
	v.y = -( ( ( 2.0f * mousePosition.y ) /  screenHeight) - 1 ) / matProj._22;
	v.z =  1.0f;

	D3DXMATRIX m;
	D3DXVECTOR3 rayOrigin,rayDirection;
	D3DXMatrixInverse(&m, NULL, &matView);
	// Transform the screen space pick ray into 3D space
	rayDirection.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
	rayDirection.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
	rayDirection.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
	rayOrigin.x = m._41;
	rayOrigin.y = m._42;
	rayOrigin.z = m._43;

	BOOL hasHit = false;

	if(displayUnitList) {
		list<Unit>::iterator uIter = UnitList.begin();
		for(;uIter != UnitList.end(); uIter++) {
			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&uIter->getMMatrix());

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDirection,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			// We can now call the intersect function on our untransformed graphic mesh data:
			float distanceToCollision;
			D3DXIntersect(uIter->getMeshSysMem(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);
		
			if(hasHit) {
				delete selectedUnit;
				selectedUnit = new Unit(uIter->getModelFile(), uIter->getDataFile(), uIter->getIconLocation(),
					uIter->getName(), uIter->getAlignment(), uIter->getTMatrix(), uIter->getValue(), 
					uIter->getCanMove(), uIter->getStance());
				selectedUnit->initializeObject(pd3dDevice);
				selectedUnit->setAllMatrix(uIter->getMMatrix(), uIter->getTMatrix(), uIter->getSMatrix(), uIter->getRMatrix(), 
					uIter->getRXMatrix(), uIter->getRYMatrix(), uIter->getRZMatrix());
				selectedUnit->updateMesh();
				selectedUnit->setLevel(uIter->getLevel());
				selectedUnit->setGroup(uIter->getGroup());
				cState = UNIT_SELECTED;
				break;
			}
		}
		if(hasHit) {
			UnitList.erase(uIter);
			return true;
		}
	}

	if(displayEnvList) {
		list<Env>::iterator eIter = EnvList.begin();
		for(;eIter != EnvList.end(); eIter++) {
			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&eIter->getMMatrix());

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDirection,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			// We can now call the intersect function on our untransformed graphic mesh data:
			float distanceToCollision;
			D3DXIntersect(eIter->getMeshSysMem(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);
		
			if(hasHit) {
				delete selectedEnv;
				selectedEnv = new Env(eIter->getModelFile(), eIter->getDataFile(), eIter->getIconLocation(),
					eIter->getName(), eIter->getAlignment(), eIter->getTMatrix(), eIter->getValue());
				selectedEnv->initializeObject(pd3dDevice);
				selectedEnv->setAllMatrix(eIter->getMMatrix(), eIter->getTMatrix(), eIter->getSMatrix(), eIter->getRMatrix(), 
					eIter->getRXMatrix(), eIter->getRYMatrix(), eIter->getRZMatrix());
				selectedEnv->updateMesh();
				cState = ENV_SELECTED;
				break;
			}
		}
		if(hasHit) {
			EnvList.erase(eIter);
			return true;
		}
	}

	if(displayMiscList) {
		list<Misc>::iterator mIter = MiscList.begin();
		for(;mIter != MiscList.end(); mIter++) {
			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&mIter->getMMatrix());

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDirection,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			// We can now call the intersect function on our untransformed graphic mesh data:
			float distanceToCollision;
			D3DXIntersect(mIter->getMeshSysMem(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);
		
			if(hasHit) {
				delete selectedMisc;
				selectedMisc = new Misc(mIter->getModelFile(), mIter->getDataFile(), mIter->getIconLocation(),
					mIter->getName(), mIter->getAlignment(), mIter->getTMatrix(), 
					mIter->getPurpose(), mIter->getHRadius(), mIter->getOrder(), mIter->getType());
				selectedMisc->initializeObject(pd3dDevice);
				selectedMisc->setAllMatrix(mIter->getMMatrix(), mIter->getTMatrix(), mIter->getSMatrix(), mIter->getRMatrix(), 
					mIter->getRXMatrix(), mIter->getRYMatrix(), mIter->getRZMatrix());
				selectedMisc->updateMesh();
				selectedMisc->setGroup(mIter->getGroup());
				selectedMisc->setSoundFile(mIter->getSoundFile());
				cState = MISC_SELECTED;
				break;
			}
		}
		if(hasHit) {
			MiscList.erase(mIter);
			return true;
		}
	}

	return false;
}

bool mousePicking2(void) {
	D3DXVECTOR3 v;
	v.x =  (((2.0f*mousePosition.x) / screenWidth)-1) / matProj._11;
	v.y = -(((2.0f*mousePosition.y) / screenHeight)-1) / matProj._22;
	v.z =  1.0f;

	D3DXMATRIX m;
	D3DXVECTOR3 rayOrigin,rayDirection;
	D3DXMatrixInverse(&m, NULL, &matView);
	// Transform the screen space pick ray into 3D space
	rayDirection.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
	rayDirection.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
	rayDirection.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
	rayOrigin.x = m._41;
	rayOrigin.y = m._42;
	rayOrigin.z = m._43;

	BOOL hasHit = false;

	if(displayUnitList) {
		list<Unit>::iterator uIter = UnitList.begin();
		for(;uIter != UnitList.end(); uIter++) {
			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&uIter->getMMatrix());

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDirection,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			// We can now call the intersect function on our untransformed graphic mesh data:
			float distanceToCollision;
			D3DXIntersect(uIter->getMeshSysMem(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);
		
			if(hasHit) {
				cState = UNITS_MENU;
				delete selectedUnit;
				selectedUnit = new Unit();
				break;
			}
		}
		if(hasHit) {
			UnitList.erase(uIter);
			return true;
		}
	}

	if(displayEnvList) {
		list<Env>::iterator eIter = EnvList.begin();
		for(;eIter != EnvList.end(); eIter++) {
			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&eIter->getMMatrix());

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDirection,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			// We can now call the intersect function on our untransformed graphic mesh data:
			float distanceToCollision;
			D3DXIntersect(eIter->getMeshSysMem(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);
		
			if(hasHit) {
				cState = ENV_MENU;
				delete selectedEnv;
				selectedEnv = new Env();
				break;
			}
		}
		if(hasHit) {
			EnvList.erase(eIter);
			return true;
		}
	}

	if(displayMiscList) {
		list<Misc>::iterator mIter = MiscList.begin();
		for(;mIter != MiscList.end(); mIter++) {
			// Use inverse of matrix
			D3DXMATRIX matInverse;
			D3DXMatrixInverse(&matInverse,NULL,&mIter->getMMatrix());

			// Transform ray origin and direction by inv matrix
			D3DXVECTOR3 rayObjOrigin,rayObjDirection;

			D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
			D3DXVec3TransformNormal(&rayObjDirection,&rayDirection,&matInverse);
			D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

			// We can now call the intersect function on our untransformed graphic mesh data:
			float distanceToCollision;
			D3DXIntersect(mIter->getMeshSysMem(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);
		
			if(hasHit) {
				cState = MISC_MENU;
				delete selectedMisc;
				selectedMisc = new Misc();
				break;
			}
		}
		if(hasHit) {
			MiscList.erase(mIter);
			return true;
		}
	}

	return false;
}

void inputSaveMapAs(void) {
	if(dKey[DIK_RETURN] & 0x80) {
		if(inputSaveElement != 0) {
			bool found = false;
			for(int i=0; i<inputSaveElement; i++) {
				if(inputSave[i] == '.') {
					inputSave[++i] = 'w';
					inputSave[++i] = 'm';
					inputSave[++i] = NULL;
					found = true;
					break;
				}
			}

			if(!found) {
				inputSave[inputSaveElement++] = '.';
				inputSave[inputSaveElement++] = 'w';
				inputSave[inputSaveElement++] = 'm';
				inputSave[inputSaveElement] = NULL;
			}

			cState = SAVING_MAP;
		} else {
			cState = CLEAR;
		}
	}

	if(dKey[DIK_BACK] & 0x80) {
		if(inputSaveElement != 0) {
			inputSaveElement--;
			inputSave[inputSaveElement] = NULL;
		}
	}

	if(dKey[DIK_A] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'A';
		} else {
			inputSave[inputSaveElement] = 'a';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_B] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'B';
		} else {
			inputSave[inputSaveElement] = 'b';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_C] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'C';
		} else {
			inputSave[inputSaveElement] = 'c';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_D] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'D';
		} else {
			inputSave[inputSaveElement] = 'd';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_E] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'E';
		} else {
			inputSave[inputSaveElement] = 'e';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_F] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'F';
		} else {
			inputSave[inputSaveElement] = 'f';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_G] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'G';
		} else {
			inputSave[inputSaveElement] = 'g';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_H] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'H';
		} else {
			inputSave[inputSaveElement] = 'h';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_I] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'I';
		} else {
			inputSave[inputSaveElement] = 'i';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_J] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'J';
		} else {
			inputSave[inputSaveElement] = 'j';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_K] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'K';
		} else {
			inputSave[inputSaveElement] = 'k';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_L] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'L';
		} else {
			inputSave[inputSaveElement] = 'l';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_M] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'M';
		} else {
			inputSave[inputSaveElement] = 'm';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_N] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'N';
		} else {
			inputSave[inputSaveElement] = 'n';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_O] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'O';
		} else {
			inputSave[inputSaveElement] = 'o';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_P] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'P';
		} else {
			inputSave[inputSaveElement] = 'p';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_Q] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'Q';
		} else {
			inputSave[inputSaveElement] = 'q';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_R] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'R';
		} else {
			inputSave[inputSaveElement] = 'r';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_S] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'S';
		} else {
			inputSave[inputSaveElement] = 's';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_T] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'T';
		} else {
			inputSave[inputSaveElement] = 't';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_U] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'U';
		} else {
			inputSave[inputSaveElement] = 'u';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_V] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'V';
		} else {
			inputSave[inputSaveElement] = 'v';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_W] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'W';
		} else {
			inputSave[inputSaveElement] = 'w';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_X] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'X';
		} else {
			inputSave[inputSaveElement] = 'x';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_Y] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'Y';
		} else {
			inputSave[inputSaveElement] = 'y';
		}
		inputSaveElement++;
	}
	if(dKey[DIK_Z] & 0x80) {
		if(dKey[DIK_LSHIFT] & 0x80) {
			inputSave[inputSaveElement] = 'Z';
		} else {
			inputSave[inputSaveElement] = 'z';
		}
		inputSaveElement++;
	}
}

void setOpenFilesList(void) {
	WIN32_FIND_DATA fileData;
	HANDLE h;
	bool finished = false;

	OpenFilesList.clear();

	h = FindFirstFile(TEXT(openFilesSearch.c_str()), &fileData);
	OpenFilesList.push_back(fileData.cFileName);
	
	while(!finished) {
		if(FindNextFile(h, &fileData)) {
			OpenFilesList.push_back(fileData.cFileName);
		} else if(!FindNextFile(h, &fileData)) {
			finished = TRUE;
		}
	}

	if(!FindClose(h)) {
		_cprintf("ERROR: Unable to close search handle\n	'Maps\\' folder may be empty\n");
	}
}

void resetOpenFileButtons(void) {
	TEXTMETRICA font;
	standardFont->GetTextMetricsA(&font);
	int startingX5 = (screenWidth/2) - (OPEN_MAP_BOX_WIDTH/2);
	int startingY5 = (screenHeight/2) - (OPEN_MAP_BOX_HEIGHT/2);

	OpenMapButtons.clear();

	BUTTON tempButton;
	tempButton.x = (startingX5 + (OPEN_MAP_BOX_WIDTH/2)) - (SUBMENU_BUTTON_WIDTH);
	tempButton.y = startingY5 + 50;
	tempButton.w = SUBMENU_BUTTON_WIDTH;
	tempButton.h = SUBMENU_BUTTON_HEIGHT;
	tempButton.bCmd = SCROLL_UP;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/ButtonUP.png", tempButton.w, tempButton.h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &tempButton.pic);
	OpenMapButtons.push_back(tempButton);

	if(!OpenFilesList.empty()) {
		if(OpenFilesList.size() >= OPEN_MAP_NUM_DISPLAY) {
			for(int i=0; i<OPEN_MAP_NUM_DISPLAY; i++) {
				tempButton.x = startingX5;
				tempButton.y = OpenMapButtons.back().y + OpenMapButtons.back().h;
				tempButton.w = OPEN_MAP_BOX_WIDTH;
				tempButton.h = font.tmHeight;
				tempButton.bCmd = OPEN_MAP_ICON_BUTTON;
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/ButtonUP.png", tempButton.w, tempButton.h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
											D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &tempButton.pic);
				OpenMapButtons.push_back(tempButton);
			}
		} else if(OpenFilesList.size() < OPEN_MAP_NUM_DISPLAY) {
			for(int i=0; i!=OpenFilesList.size(); i++) {
				tempButton.x = startingX5;
				tempButton.y = OpenMapButtons.back().y + OpenMapButtons.back().h;
				tempButton.w = OPEN_MAP_BOX_WIDTH;
				tempButton.h = font.tmHeight;
				tempButton.bCmd = OPEN_MAP_ICON_BUTTON;
				D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/ButtonUP.png", tempButton.w, tempButton.h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
											D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &tempButton.pic);
				OpenMapButtons.push_back(tempButton);
			}
		}
	}

	tempButton.x = OpenMapButtons.front().x;
	tempButton.y = OpenMapButtons.back().y + OpenMapButtons.back().h;
	tempButton.w = SUBMENU_BUTTON_WIDTH;
	tempButton.h = SUBMENU_BUTTON_HEIGHT;
	tempButton.bCmd = SCROLL_DOWN;
	D3DXCreateTextureFromFileEx(pd3dDevice, "Graphics/Editor Icons/ButtonDOWN.png", tempButton.w, tempButton.h, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, 
								D3DPOOL_DEFAULT,SPRITE_FILTER, SPRITE_MIP_FILTER, BUTTON_TRANSPARENCY, NULL, NULL, &tempButton.pic);
	OpenMapButtons.push_back(tempButton);
}

void eraseLists(void) {
	initializeLight();
	//pd3dDevice->SetLight(0, &light);

	MapObject->setObjectScale((float)mapScaleX, (float)mapScaleY, (float)mapScaleZ);

	drawingSequenceType.clear();

	list<Unit>::iterator uI = UnitList.begin();
	for(;uI != UnitList.end(); uI++) {
		uI->deallocateMemory();
	}
	UnitList.clear();

	list<Env>::iterator eI = EnvList.begin();
	for(;eI != EnvList.end(); eI++) {
		eI->deallocateMemory();
	}
	EnvList.clear();

	list<Misc>::iterator mI = MiscList.begin();
	for(;mI != MiscList.end(); mI++) {
		mI->deallocateMemory();
	}
	MiscList.clear();
}

/********************************
Importing/Exporting Map Functions
********************************/

void saveToFile(void) {
	char fileLoc[FILE_NAME_LENGTH_MAX] = "Maps\\";
	for(int i=0; i<FILE_NAME_LENGTH_MAX; i++) {
		if(fileLoc[i] == NULL) {
			for(int j=0; j<inputSaveElement; j++) {
				fileLoc[i] = inputSave[j];
				i++;
			}
			fileLoc[i] = NULL;
			break;
		}
	}

	// create a file
	fstream f;
	f.open(fileLoc);
	f.close();
	
	// output to file
	ofstream out;
	out.open(fileLoc, fstream::out | fstream::trunc); // trunc will erase all previous data

	out << PROGRAM_VERSION << "\n";
	
	// output light information
	out << "<LIGHT>\n";
	out << lightDir.x << " ";
	out << lightDir.y << " ";
	out << lightDir.z << "\n";
	out << light.Diffuse.r << " ";
	out << light.Diffuse.g << " ";
	out << light.Diffuse.b << "\n";
	out << light.Specular.r << " ";
	out << light.Specular.g << " ";
	out << light.Specular.b << "\n";
	out << light.Ambient.r << " ";
	out << light.Ambient.g << " ";
	out << light.Ambient.b << "\n";

	// output map information
	out << "<MAP>\n";
	MapObject->saveInfoToFile(out);

	list<Unit>::iterator uI = UnitList.begin();
	for(;uI != UnitList.end(); uI++) {
		out << "<UNIT>\n";
		uI->saveInfoToFile(out);
	}
	list<Env>::iterator eI = EnvList.begin();
	for(;eI != EnvList.end(); eI++) {
		out << "<ENV>\n";
		eI->saveInfoToFile(out);
	}
	list<Misc>::iterator mI = MiscList.begin();
	for(;mI != MiscList.end(); mI++) {
		out << "<MISC>\n";
		mI->saveInfoToFile(out);
	}

	out << "<END>\n";

	out.close();
}

void openMap(string mapLoc) {
	eraseLists();

	string tempLocation = openFilesLocation;
	tempLocation.append(mapLoc);
	fstream in(tempLocation.c_str());
	
	// temporary variables to hold imported information

	string version;
	
	// Object
	// read order: mFile, dFile, iLoc, name, align, posXYZ, scXYZ, roXYZ
	string tag, mFile, dFile, iLoc, name, align;
	float locX, locY, locZ, scX, scY, scZ, roX, roY, roZ;
	// Unit
	// read order: lvl, val, cMove, st, grp
	string cMove, st;
	int lvl, val, grp;
	// Env
	// read order: val
	//int val;
	// Misc
	// read order: pur, hRad, ord, type, grp, sFile
	string pur, type;
	int ord;// grp;
	float hRad;
	string sFile;
	
	in >> version;

	in >> tag;
	while(tag != "<END>") {
		if(tag == "<LIGHT>") {
			in >> lightDir.x;
			in >> lightDir.y;
			in >> lightDir.z;
			in >> light.Diffuse.r;
			in >> light.Diffuse.g;
			in >> light.Diffuse.b;
			in >> light.Specular.r;
			in >> light.Specular.g;
			in >> light.Specular.b;
			in >> light.Ambient.r;
			in >> light.Ambient.g;
			in >> light.Ambient.b;
			D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &lightDir);
			pd3dDevice->SetLight(0, &light);
		}
		if(tag == "<MAP>") {
			in >> mFile;
			in >> dFile;
			in >> iLoc;
			in >> name;
			in >> align;
			in >> locX;
			in >> locY;
			in >> locZ;
			in >> scX;
			in >> scY;
			in >> scZ;
			in >> roX;
			in >> roY;
			in >> roZ;

			MapObject->deallocateMemory();
			delete MapObject;

			D3DXMATRIX tMatrix;
			D3DXMatrixTranslation(&tMatrix, 0.0f, 0.0f, 0.0f);

			MapObject = new Object(mFile, dFile, iLoc, name, align, tMatrix);
			MapObject->setObjectScale((float)scX,(float)scY,(float)scZ);
			if(!MapObject->initializeObject(pd3dDevice)) {
				_cprintf("ERROR: Could not load map mesh!\n");
			}
		}
		if(tag == "<UNIT>") {
			in >> mFile;
			in >> dFile;
			in >> iLoc;
			in >> name;
			in >> align;
			in >> locX;
			in >> locY;
			in >> locZ;
			in >> scX;
			in >> scY;
			in >> scZ;
			in >> roX;
			in >> roY;
			in >> roZ;
			in >> lvl;
			in >> val;
			in >> cMove;
			in >> st;
			in >> grp;

			D3DXMATRIX tMatrix;
			D3DXMatrixTranslation(&tMatrix, locX, locY, locZ);

			Unit tempUnit(mFile, dFile, iLoc, name, align, tMatrix, val, cMove, st);
			tempUnit.setLevel(lvl);
			tempUnit.setGroup(grp);
			tempUnit.setObjectScale(scX, scY, scZ);
			tempUnit.rotateObject(roX, roY, roZ);
			if(!tempUnit.initializeObject(pd3dDevice)) {
				_cprintf("ERROR: Could not load unit mesh!\n");
			}
			UnitList.push_front(tempUnit);
		}
		if(tag == "<ENV>") {
			in >> mFile;
			in >> dFile;
			in >> iLoc;
			in >> name;
			in >> align;
			in >> locX;
			in >> locY;
			in >> locZ;
			in >> scX;
			in >> scY;
			in >> scZ;
			in >> roX;
			in >> roY;
			in >> roZ;
			in >> val;

			D3DXMATRIX tMatrix;
			D3DXMatrixTranslation(&tMatrix, locX, locY, locZ);
			
			Env tempEnv(mFile, dFile, iLoc, name, align, tMatrix, val);
			tempEnv.setObjectScale(scX, scY, scZ);
			tempEnv.rotateObject(roX, roY, roZ);
			if(!tempEnv.initializeObject(pd3dDevice)) {
				_cprintf("ERROR: Could not load env mesh!\n");
			}
			EnvList.push_front(tempEnv);
		}
		if(tag == "<MISC>") {
			in >> mFile;
			in >> dFile;
			in >> iLoc;
			in >> name;
			in >> align;
			in >> locX;
			in >> locY;
			in >> locZ;
			in >> scX;
			in >> scY;
			in >> scZ;
			in >> roX;
			in >> roY;
			in >> roZ;
			in >> pur;
			in >> hRad;
			in >> ord;
			in >> type;
			in >> grp;
			if(version == PROGRAM_VERSION) {
				in >> sFile;
			}

			D3DXMATRIX tMatrix;
			D3DXMatrixTranslation(&tMatrix, locX, locY, locZ);
			Misc tempMisc(mFile, dFile, iLoc, name, align, tMatrix, pur, hRad, ord, type);
			tempMisc.setGroup(grp);
			if(version == PROGRAM_VERSION) {
				tempMisc.setSoundFile(sFile);
			}
			tempMisc.setObjectScale(scX, scY, scZ);
			tempMisc.rotateObject(roX, roY, roZ);
			if(!tempMisc.initializeObject(pd3dDevice)) {
				_cprintf("ERROR: Could not load misc mesh!\n");
			}
			MiscList.push_front(tempMisc);
		}

		in >> tag;
	}

	in.close();
}